package com.qianguo.shop.api;

import android.content.Context;
import android.text.TextUtils;
import android.support.v4.util.ArrayMap;

import com.eva.androidlibrary.api.CommonApi;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.entity.ResultData;
import com.qianguo.shop.entity.CategoryListBean;
import com.qianguo.shop.entity.MailmenListBean;
import com.eva.androidlibrary.api.entity.OrderInfo;
import com.qianguo.shop.entity.OrderList;
import com.eva.androidlibrary.api.entity.Product;
import com.eva.androidlibrary.api.entity.ProductList;
import com.qianguo.shop.entity.PublishResult;
import com.qianguo.shop.entity.RushResult;
import com.qianguo.shop.entity.ShopAssistantListBean;
import com.eva.androidlibrary.api.entity.ShopInfo;
import com.eva.androidlibrary.api.entity.ShopListBean;
import com.qianguo.shop.entity.ShopPosition;


/**
 * Class description
 *
 * @author GUXIA318
 * @date 2015-11-04 14:09
 */
public class ShopApi extends CommonApi {

    private static ShopApi sInstance = null;

    // 公司IP
//    private  String hostUrl = "http://10.59.90.54:8080";

    // 家里IP
//    private  String hostUrl = "http://192.168.0.101:8080";

    public ShopApi(Context context) {
        super(context);
    }

    /**
     * 初次使用时调用此方法获取实例
     *
     * @return
     */
    public synchronized static ShopApi getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new ShopApi(context);
        }
        return sInstance;
    }

    /**
     * 获取店铺列表
     *
     * @param token
     * @param page
     * @param perpage
     * @param callback
     */
    public void getShopList(String token, int page, int perpage, HttpRequestCallback<ShopListBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        post(ShopListBean.class, hostUrl, "/1.0/shop_myshop/list", params, callback);
    }

    /**
     * 编辑店铺信息
     *
     * @param token
     * @param shop
     * @param callback
     */
    public void editShopInfo(String token, ShopInfo shop, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shop.getId());
        params.put("name", shop.getName());
        params.put("address", shop.getAddress());
        params.put("mobile", shop.getMobile());
        if (shop.getSaleDaily() != 0) {
            params.put("day_sales", Integer.toString(shop.getSaleDaily()));
        }
        params.put("service_time", shop.getOpenTime());
        if (!TextUtils.isEmpty(shop.getLogo())) {
            params.put("img_url", shop.getLogo());
        }
        params.put("intro", shop.getIntro());
        params.put("longitude", shop.getLongitude());
        params.put("latitude", shop.getLatitude());
        post(ResultData.class, hostUrl, "/1.0/shop_info/edit", params, callback);
    }

    /**
     * 添加店铺
     *
     * @param token
     *          用户token
     */
    public void addShop(String token, ShopInfo shop, HttpRequestCallback<ShopInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("name", shop.getName());
        params.put("address", shop.getAddress());
        params.put("mobile", shop.getMobile());
        if (shop.getSaleDaily() != 0) {
            params.put("day_sales", Integer.toString(shop.getSaleDaily()));
        }

        params.put("service_time", shop.getOpenTime());
        params.put("intro", shop.getIntro());
        params.put("latitude", shop.getLatitude());
        params.put("longitude", shop.getLongitude());
        post(ShopInfo.class, hostUrl, "/1.0/shop_info/add", params, callback);
    }

    /**
     * 获取店员列表
     * 
     * @param shopID
     *          店铺ID
     */
    public void getShopAssistants(String token, String shopID, int page, int perPage, HttpRequestCallback<ShopAssistantListBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopID);

        if (page != 0) {
            params.put("page", Integer.toString(page));
        }

        if (perPage != 0) {
            params.put("perpage", Integer.toString(perPage));
        }
        post(ShopAssistantListBean.class, hostUrl, "/1.0/clerk_info/list", params, callback);
    }

    /**
     * 获取配送员列表
     *
     * @param token
     * @param shopID
     * @param page
     * @param perPage
     * @param callback
     */
    public void getMailmanList(String token, String shopID, int page, int perPage, HttpRequestCallback<MailmenListBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopID);

        if (page != 0) {
            params.put("page", Integer.toString(page));
        }

        if (perPage != 0) {
            params.put("perpage", Integer.toString(perPage));
        }
        post(MailmenListBean.class, hostUrl, "/1.0/courier_courier/list", params, callback);
    }


    /**
     * 发布订单
     *
     * @param orderId 订单id
     */
    public void publishOrder(int orderId, HttpRequestCallback<PublishResult> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("orderid", Integer.toString(orderId));
        get(PublishResult.class, hostUrl, "/publishOrder", params, callback);
    }

    /**
     * 催配送
     *
     * @param orderId 订单id
     */
    public void rushOrder(int orderId, HttpRequestCallback<RushResult> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("orderid", Integer.toString(orderId));
        get(RushResult.class, hostUrl, "/rushOrder", params, callback);
    }

    /**
     * 称重
     *
     * @param token
     * @param ordersn
     * @param price
     * @param callback
     */
    public void weight(String token, String ordersn, int price, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", ordersn);
        params.put("price", Integer.toString(price));
        post(ResultData.class, hostUrl, "/1.0/order_merchant/weigh", params, callback);
    }

    /**
     * 获取订单
     * @param token
     * @param orderId
     * @param callback
     */
    public void getOrder(String token, String orderId, HttpRequestCallback<OrderInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("ordersn", orderId);
        post(OrderInfo.class, hostUrl, "/1.0/order_merchant/view", params, callback);
    }

    /**
     * 获取订单列表
     *
     * @param token
     * @param shopId
     * @param type
     * @param page
     * @param perPage
     * @param callback
     */
    public void getOrders(String token, String shopId, int type, int page, int perPage, HttpRequestCallback<OrderList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopId);
        params.put("type", Integer.toString(type));
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perPage));
        post(OrderList.class, hostUrl, "/1.0/order_merchant/list", params, callback);
    }

    /**
     * 添加店员
     *
     * @param shopID
     *          店铺ID
     * @param name
     *          店员名
     * @param mobile
     *          店员手机号
     * @param sex
     *          店员性别
     * @param avatarUrl
     *          店员头像ID
     * @param positionID
     *          店员职位ID
     * @param callback
     *
     */
    public void addShopAssistant(String token, String shopID, String name, String mobile, String sex, String avatarUrl, String positionID, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopID);
        params.put("realname", name);
        params.put("phone", mobile);
        params.put("sex", sex);
        params.put("role", positionID);

        // TODO: 16/5/27 key
        if (!TextUtils.isEmpty(avatarUrl)) {
            params.put("avatarID", avatarUrl);
        }
        post(ResultData.class, hostUrl, "/1.0/clerk_info/add", params, callback);
    }

    /**
     * 获取产品列表
     *
     * @param token
     * @param shopID
     *          店铺ID
     * @param callback
     */
    public void getProducts(String token, String shopID, int page, int perpage, HttpRequestCallback<ProductList> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        if (!TextUtils.isEmpty(token)) {
            params.put("token", token);
        }
        params.put("shopid", shopID);
        params.put("page", Integer.toString(page));
        params.put("perpage", Integer.toString(perpage));
        post(ProductList.class, hostUrl, "/1.0/search_merchantproduct/list", params, callback);
    }

    /**
     * 添加商品
     *
     * @param product
     * @param callback
     */
    public void addProduct(String token, Product product, HttpRequestCallback<Product> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", product.getShopId());
        params.put("name", product.getName());
        if (!TextUtils.isEmpty(product.getRegion())) {
            params.put("region", product.getRegion());
        }
        params.put("unit", product.getUnit());
        params.put("price", Float.toString(product.getPrice()));

        if (product.getMarketPrice() > 0) {
            params.put("market_price", Float.toString(product.getMarketPrice()));
        }

        params.put("categoryid", Integer.toString(product.getCategoryId()));

        if (!TextUtils.isEmpty(product.getIntro())) {
            params.put("detail", product.getIntro());
        }

        if (!TextUtils.isEmpty(product.getCover())) {
            params.put("cover", product.getCover());
        }

        post(Product.class, hostUrl, "/1.0/item_product/add", params, callback);
    }

    /**
     * 编辑产品
     *
     * @param token
     * @param origin
     * @param copy
     * @param callback
     */
    public void editProduct(String token, Product origin, Product copy, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("productid", copy.getId());
        if (!origin.getName().equals(copy.getName())) {
            params.put("name", copy.getName());
        }

        if (!origin.getRegion().equals(copy.getRegion())) {
            params.put("region", copy.getRegion());
        }

        if (!origin.getUnit().equals(copy.getUnit())) {
            params.put("unit", copy.getUnit());
        }

        if (origin.getPrice() != copy.getPrice()) {
            params.put("price", Float.toString(copy.getPrice()));
        }

        if (origin.getMarketPrice() != copy.getMarketPrice()) {
            params.put("market_price", Float.toString(copy.getMarketPrice()));
        }

        if (origin.getCategoryId() != copy.getCategoryId()) {
            params.put("categoryid", Integer.toString(copy.getCategoryId()));
        }

        if (!origin.getIntro().equals(copy.getIntro())) {
            params.put("detail", copy.getIntro());
        }

        if (!origin.getCover().equals(copy.getCover())) {
            params.put("cover", copy.getCover());
        }

        // TODO: 16/6/15 pics
        post(ResultData.class, hostUrl, "/1.0/item_product/edit", params, callback);
    }

    /**
     * 获取职位
     *
     * @param callback
     */
    public void getRoles(HttpRequestCallback<ShopPosition> callback) {
        get(ShopPosition.class, hostUrl, "/1.0/config_clerk/get", null, callback);
    }

    /**
     * 获取商品品类列表
     *
     * @param shopId
     * @param callback
     */
    public void getCategories(String shopId, HttpRequestCallback<CategoryListBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        if (TextUtils.isEmpty(shopId)) {
            shopId = "0";
        }
        params.put("shopid", shopId);
        post(CategoryListBean.class, hostUrl, "/1.0/shop_category/shopcategory", params, callback);
    }


}
